package cn.edu.hqu.cst.sensordemo;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path="/app/LinearAccelActivity")
public class LinearAccel extends AppCompatActivity implements SensorEventListener {

    @BindView(R.id.lc_linear_accel)
    LineChart lc_linear_accel;
    SensorManager sensorManager=null;
    private int timeIndex=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linear_accel);
        ButterKnife.bind(this);
        sensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
        lc_linear_accel.setTouchEnabled(false);
        lc_linear_accel.getXAxis().setEnabled(true);
        lc_linear_accel.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lc_linear_accel.getAxisLeft().setEnabled(true);
        lc_linear_accel.getAxisRight().setEnabled(false);
        List<Entry> XEntries=new ArrayList<>();
        List<Entry> YEntries=new ArrayList<>();
        List<Entry> ZEntries=new ArrayList<>();
        LineDataSet XDataset=new LineDataSet(XEntries,"X");
        LineDataSet YDataset=new LineDataSet(YEntries,"Y");
        LineDataSet ZDataset=new LineDataSet(ZEntries,"Z");
        XDataset.setDrawCircles(false);
        YDataset.setDrawCircles(false);
        ZDataset.setDrawCircles(false);
        XDataset.setColor(Color.rgb(255,0,0));
        YDataset.setColor(Color.rgb(0,255,0));
        ZDataset.setColor(Color.rgb(0,0,255));
        LineData lineData=new LineData();
        lineData.addDataSet(XDataset);
        lineData.addDataSet(YDataset);
        lineData.addDataSet(ZDataset);
        lc_linear_accel.setData(lineData);
    }
    @Override
    protected void onPostResume() {
        super.onPostResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),SensorManager.SENSOR_DELAY_NORMAL);

    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        float values[]=sensorEvent.values;
        LineData lineData=lc_linear_accel.getLineData();
        if(lineData!=null){
            LineDataSet XLineDataSet= (LineDataSet) lineData.getDataSetByLabel("X",true);
            LineDataSet YLineDataSet=(LineDataSet)lineData.getDataSetByLabel("Y",true);
            LineDataSet ZLineDataSet=(LineDataSet)lineData.getDataSetByLabel("Z",true);
            XLineDataSet.addEntry(new Entry(timeIndex++,values[0]));
            YLineDataSet.addEntry(new Entry(timeIndex++,values[1]));
            ZLineDataSet.addEntry(new Entry(timeIndex++,values[2]));
            if(XLineDataSet.getEntryCount()<200){
                lc_linear_accel.getXAxis().setAxisMaximum(200);
            }else{
                XLineDataSet.removeFirst();
                YLineDataSet.removeFirst();
                ZLineDataSet.removeFirst();
                lc_linear_accel.getXAxis().setAxisMaximum(XLineDataSet.getXMax());

            }
            lc_linear_accel.getAxisLeft().setAxisMaximum(XLineDataSet.getYMax());
            lc_linear_accel.getAxisLeft().setAxisMinimum(XLineDataSet.getYMin());
            lc_linear_accel.getXAxis().setAxisMinimum(XLineDataSet.getXMin());
            lc_linear_accel.notifyDataSetChanged();
            lc_linear_accel.invalidate();
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}
